package co.fitstart.signin.web.controller;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.demo2do.core.entity.result.HttpResult;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.StudentSigninRecord;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.signin.service.PrincipalService;
import co.fitstart.signin.service.SigninService;
import co.fitstart.signin.web.resolver.Secure;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("")
public class SigninController {
    
    @Autowired
    private SigninService signinService;
    
    @Autowired
    private PrincipalService principalService;
    
    private static final Logger logger = LoggerFactory.getLogger(SigninController.class);
    
    /**
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(path = "/is-overdue", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult isOverdue() {
      HttpResult httpResult = new HttpResult();
      
      return httpResult.success().message("获取数据成功");
    }
    
    /**
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(path = "/signin-data", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult list(@RequestParam("siteId") Long siteId) {
      HttpResult httpResult = new HttpResult();
      
      List<StudentSigninRecord> records = signinService.list();
      
      return httpResult.success().data("records", records);
    }
    
    /**
     * 
     * @param siteId
     * @param message
     * @return
     */
    @RequestMapping(path = "/signin-record", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult signinRecord(@RequestParam("siteId") Long siteId,
                                                 @RequestParam("message") String message) {
        HttpResult httpResult = new HttpResult();
        
        try {
            // 学员课程签到记录
            StudentSigninRecord signinRecord = signinService.load(siteId, message);
            
            // 是否需要填写三维数据
            if(signinRecord != null) {
            	
                CourseSchedule courseSchedule = signinRecord.getCourseSchedule();
                
                boolean couldEdit = signinService.isBwhNeeded(courseSchedule);
                
                // 签到记录设为已展示
                signinService.showSigninRecord(signinRecord);
                
                httpResult.success().message("获取数据成功")
                                    .data("signinRecord", signinRecord)
                                    .data("courseName", courseSchedule.getCourse().getName())
                                    .data("courseSchedule", courseSchedule)
                                    .data("couldEdit", couldEdit);
                
                if(signinRecord.getTargetType() != null) {
                    httpResult.data("digital", signinRecord.getTargetType().isDigital())
                              .data("targetTypeDescription", signinRecord.getTargetType().getDescription());
                } else {
                    httpResult.data("digital", false);
                }
            } else {
                httpResult.fail("500").message("学员签到数据为空");
            }
            
        } catch (Exception e) {
            logger.error("sigin error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
    
    /**
     * 
     * @param scheduleId
     * @param bwhRecord
     * @param id
     * @return
     */
    @RequestMapping(path = "/signin", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult signin(@RequestParam("scheduleId") Long scheduleId,
                             BwhRecord bwhRecord,
                             @Secure(property = "id") Long id) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Principal siteServer = principalService.load(Principal.class, id);
            CourseSchedule courseSchedule = signinService.loadCourseSchedule(scheduleId);
            
            signinService.signin(courseSchedule, siteServer, bwhRecord);
            
            httpResult.success().message("签到成功");
        } catch (Exception e) {
            logger.error("sigin error", e);
            httpResult.fail("500").message("签到失败");
        }
        
        return httpResult;
    }

}
